//
//  YSReadHelper.m
//  YSBookRead
//
//  Created by MOLBASE on 2018/11/8.
//  Copyright © 2018年 YangShen. All rights reserved.
//

#import "YSReadHelper.h"
#import <CommonCrypto/CommonDigest.h>

@implementation YSReadHelper

+ (UIImage *)screenCapture:(UIView *)view {
    UIView *sview = view;
    if (!sview) {
        sview = [[UIApplication sharedApplication] keyWindow];
    }
    UIGraphicsBeginImageContextWithOptions(sview.bounds.size, NO, 0);
    [sview.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *snap = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return snap;
}

+ (NSString *)getCurrentTimerString:(NSString *)dateFormat {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = dateFormat;
    return [dateFormatter stringFromDate:[NSDate date]];
}

+ (NSString *)md5:(NSString *)string {
    const char *original_str = [string UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(original_str, (CC_LONG)strlen(original_str), result);
    NSMutableString *hash = [NSMutableString string];
    for (int i = 0; i < 16; i++)
        [hash appendFormat:@"%02X", result[i]];
    [hash lowercaseString];
    return hash;
}

+ (NSString *)encodeToPercentEscapeString:(NSString *)input {
    return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,(__bridge CFStringRef)input, NULL,(CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8));
}

#define kiOS10_3Later ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 10.3)

/// 格式化章节内容
+ (NSString *)formartChpaterContent:(NSString *)chapterContent {
    if (!chapterContent) { return nil; }

    // 替换单换行
    chapterContent = [chapterContent stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    // 替换换行 以及 多个换行 为 换行加空格
    NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:@"\\s*\\n+\\s*" options:NSRegularExpressionCaseInsensitive error:nil];
    chapterContent = [expression stringByReplacingMatchesInString:chapterContent options:NSMatchingReportProgress range:NSMakeRange(0, chapterContent.length) withTemplate:@"\n      "];
    chapterContent = [chapterContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];;

    chapterContent = [chapterContent stringByAppendingString:@"      "];

    return chapterContent;
}

+ (NSString *)getUpdateStringWith:(NSDate *)date {
    if (!date || ![date isKindOfClass:[NSDate class]]) {
        return @"long long ago";
    } else {
        NSString *unit = nil;
        NSInteger number = 0;
        NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:date];
        if (interval > 365 * 24 * 60 * 60) {
            number = interval / (365 * 24 * 60 * 60);
            unit = @"年";
        } else if (interval > 30 * 24 * 60 * 60) {
            number = interval / (30 * 24 * 60 * 60);
            unit = @"月";
        } else if (interval > 24 * 60 * 60) {
            number = interval / (24 * 60 * 60);
            unit = @"天";
        } else if (interval > 60 * 60) {
            number = interval / (60 * 60);
            unit = @"小时";
        } else if (interval > 60) {
            number = interval / 60;
            unit = @"分钟";
        } else {
            return @"刚刚";
        }
        return [NSString stringWithFormat:@"%zi%@前",number,unit];
    }
}

+ (UIImage *)imageWithColor:(nonnull UIColor *)color size:(CGSize)size {
    CGRect rect = (CGRect){CGPointZero, size};
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}


@end
